package com.vf.admin.client.controller.ip;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.service.AppIpAuthService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.miaodong.entity.AppIpAuth;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.ip.IpUtils;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Title : </p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/9/25 15:24
 */
@Controller
@RequestMapping(value = "/api/ip")
public class IpPlatformController extends ClientBaseController {
    @Resource
    private AppIpAuthService appIpAuthService;

    @RequestMapping("/isWhiteIp")
    @ResponseBody
    public Result isWhiteIp(String account, String platform) {
        String deviceModel = request.getHeader("deviceModel");
        String versionName = request.getHeader("versionName");
        String packageName = request.getHeader(ClientConstants.packageName);
        if (ObjectUtil.isEmpty(packageName)) {
            packageName = "lyq";
        }
        String ipAddress = IpUtils.getIpAddress(request);

        //判断ip是否跟白名单内的一致
        List<AppIpAuth> ips = appIpAuthService.getIpList(ipAddress);
        List<AppIpAuth> accounts = appIpAuthService.getAccountList(account);

        //只要一个满足条件 返回true
        if (ObjectUtil.isNotEmpty(ips) || ObjectUtil.isNotEmpty(accounts)) {
            //如果ip存在 账号不存在保存
            if (ObjectUtil.isNotEmpty(ips)){
                List<AppIpAuth> ipss = appIpAuthService.getIpList(ipAddress);
                List<String> collect = ipss.stream().map(AppIpAuth::getAccount).collect(Collectors.toList());
                boolean containAccount = collect.contains(account);
                if (!containAccount){
                    //执行保存操作
                    AppIpAuth appIpAuth1 = new AppIpAuth();
                    appIpAuth1.setAccount(account);
                    appIpAuth1.setIp(ipAddress);
                    appIpAuth1.setPlatform(platform);
                    appIpAuth1.setDeviceModel(deviceModel);
                    appIpAuth1.setVersionName(versionName);
                    appIpAuth1.setPackageName(packageName);
                    appIpAuth1.setCreatetime(new Date());
                    appIpAuthService.save(appIpAuth1);
                }
            }
            //如果账号存在 ip不存在 保存
            if (ObjectUtil.isNotEmpty(account)){
                List<AppIpAuth> accountss = appIpAuthService.getAccountList(account);
                List<String> collect = accountss.stream().map(AppIpAuth::getIp).collect(Collectors.toList());
                boolean containsIps = collect.contains(ipAddress);
                if (!containsIps){
                    //执行保存操作
                    AppIpAuth appIpAuth1 = new AppIpAuth();
                    appIpAuth1.setAccount(account);
                    appIpAuth1.setIp(ipAddress);
                    appIpAuth1.setPlatform(platform);
                    appIpAuth1.setDeviceModel(deviceModel);
                    appIpAuth1.setVersionName(versionName);
                    appIpAuth1.setPackageName(packageName);
                    appIpAuth1.setCreatetime(new Date());
                    appIpAuthService.save(appIpAuth1);
                }
            }
            //如果都存在不执行操作
            return this.sucessJSON(true);
        }
        return this.sucessJSON(false);
    }
}
